﻿Imports System
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Deployment.Application
Imports System.Xml
Imports System.Xml.Serialization
Imports Framework.LeastPrivilege.Tools.ProtectedXml
Imports System.Security.Cryptography

Public Class IsoDBSettings
    Shared Sub New()
    End Sub

    Public Shared Function Save() As Boolean
        Try
            If IsoDBSettings.DBPass <> "" AndAlso IsoDBSettings.DBPath <> "" Then

                Dim isoStorage As IsolatedStorageFile

                isoStorage = IsoStorageSource

                Using isoStorage
                    Using isoStream As New IsolatedStorageFileStream("b2bsettings.xml", FileMode.Create, isoStorage)
                        Dim config As New Config
                        config.Path = DBPath
                        config.Pass = DBPass
                        Dim ser As New XmlSerializer(GetType(Config))
                        ser.Serialize(isoStream, config)

                        _SettingsLoaded = True
                        config = Nothing
                        ser = Nothing
                    End Using
                End Using

                Return True

            End If
        Catch ex As Exception
            Return False
        End Try
        Return False
    End Function

    Public Shared Function Load() As Boolean
        Try

            Dim isoStorage As IsolatedStorageFile

            isoStorage = IsoStorageSource

            Using isoStorage
                Using isoStream As New IsolatedStorageFileStream("b2bsettings.xml", FileMode.Open, isoStorage)
                    _DBPass = ""
                    _DBPath = ""

                    Dim ser As New XmlSerializer(GetType(Config))
                    Dim config As Config = DirectCast(ser.Deserialize(isoStream), Config)
                    DBPath = config.Path
                    DBPass = config.Pass
                    config = Nothing
                    ser = Nothing
                End Using

                If _DBPass <> "" And _DBPath <> "" Then
                    _SettingsLoaded = True
                    Return True
                Else
                    _SettingsLoaded = False
                    Return False
                End If
            End Using


        Catch ex As Exception
            Delete() ' Delete broken settings if we fail to load them
        End Try

        _SettingsLoaded = False
        Return False

    End Function

    Private Shared Function Delete() As Boolean
        Try
            Dim isoStorage As IsolatedStorageFile
            isoStorage = IsoStorageSource

            Using isoStorage
                isoStorage.DeleteFile("b2bsettings.xml")
                _SettingsLoaded = False
            End Using

            Return True
        Catch ex As Exception
        End Try
        Return False
    End Function

    Private Shared _IsoType As IsoTypes
    Private Shared _SettingsLoaded As Boolean
    Private Shared _DBPath As String
    Private Shared _DBPass As String

    Public Shared Property IsoType() As IsoTypes
        Get
            Return _IsoType
        End Get
        Set(ByVal value As IsoTypes)
            _IsoType = value
        End Set
    End Property

    Public Shared Property SettingsLoaded() As Boolean
        Get
            Return _SettingsLoaded
        End Get
        Set(ByVal value As Boolean)
            _SettingsLoaded = value
        End Set
    End Property

    Public Shared Property DBPath() As String
        Get
            Return _DBPath
        End Get
        Set(ByVal value As String)
            _DBPath = value
        End Set
    End Property

    Public Shared Property DBPass() As String
        Get
            Return _DBPass
        End Get
        Set(ByVal value As String)
            _DBPass = value
        End Set
    End Property

    Private Shared ReadOnly Property IsoStorageSource() As IsolatedStorageFile
        Get ' ClickOnce or debugmode
            If (ApplicationDeployment.IsNetworkDeployed) Then
                _IsoType = IsoTypes.AppDomain
                Return IsolatedStorageFile.GetUserStoreForApplication()
            Else
                _IsoType = IsoTypes.User
                Return IsolatedStorageFile.GetUserStoreForDomain
            End If
        End Get
    End Property

    Public Enum IsoTypes
        AppDomain = 0
        User = 1
    End Enum

    <ProtectedXml(DataProtectionScope.CurrentUser)> _
    Public Class Config
        Inherits ProtectedXmlBase
        <ProtectedField()> _
        Public Path As String

        <ProtectedField()> _
        Public Pass As String
    End Class

End Class

